import utils from "../../utils/index";
import {fansModel} from "../../apis/index"

import regeneratorRuntime from '../../libs/wxPromise.min';
Page({

  data: {
    list:[{name:'活跃达人',type:'1'},{name:'贡献达人',type:'2'},{name:'管理员',type:'3'}],
    tabIndex:0,
    page:{
      page:1,
      pageSize:10
    },
    memberList:[],
    tid:'',
    uuid:''
  },
  mixins: [require('../../myMixin.js')],
  async onLoad(options) {

    let {tid='',my_level=''} = options
    if(!tid){
      utils.navigateBack({
        delta:1
      })
      return
    }
    this.data.tid = tid
    let uuid =await this.getuuid()
    console.log(uuid)
    this.setData({
      uuid,
      my_level
    })
  },
  mixins: [require('../../myMixin.js')],
  onReady: function () {

  },
  onShow: function () {
    this.getInitStarList()
  },
  tabClick(e){
    let index = e.detail.index
    this.setData({
      tabIndex:index
    })
    this.getInitStarList()
  },
  getInitStarList(){

    this.data.memberList = []
    this.data.page.page = 1
    this.getDataList()
  },
  getDataList(){
    let {page,pageSize} = this.data.page
    let {memberList,list,tabIndex,tid} = this.data
    let param = {
      page:page,
      pageSize:pageSize,
      tid:tid,
      type:list[tabIndex].type
    }
    utils.showLoading()
    fansModel.fansMemberList(param).then(res=>{
      let _list = res.list
      memberList = memberList.concat(_list)
      this.computedPage('page',res.total)
      this.setData({
        memberList:memberList
      })  
      utils.hideAll()
    }).catch(()=>{
      utils.hideAll()
    })
  },
  moreList1Data(){
    this.loadMoreData('page',this.getDataList)
  },
  hideModal(){
    this.setData({
      showModal:false
    })
  },
  showModal(e){
    let {item,index} = e.currentTarget.dataset

    this.setData({
      modalItem:item
    },()=>{
      this.data.modalIndex = index
      this.setData({
        showModal:true
      })
    })

  },
  adminPermission(e){
    let operator = e.currentTarget.dataset.operator
    let {tid:fid,modalItem,modalIndex,memberList,tabIndex} = this.data
    let type = ''
    if(operator==0){
      type = 0
    }else if(operator==1){
      let {level} = modalItem
      if(level==2){
        type ='1'
      }else if(level==3){
        type = '2'
      }
    }
    let param = {
      uid:modalItem.id,
      fid,
      type
    }
    fansModel.adminPermission(param).then(res=>{
      if(operator==0){
        memberList.splice(modalIndex,1)
        this.setData({
          memberList,
          showModal:false
        })
      }else if(operator==1){
        let {level} = modalItem
        if(level==2){//撤销管理员
          type ='1'
          modalItem.level = 3
        }else if(level==3){ // 晋升管理员
          type = '2'
          modalItem.level = 2
        }
        if(tabIndex==2){
          memberList.splice(modalIndex,1)
          this.setData({
            modalItem,
            memberList
          })
        }else{
          this.setData({
            modalItem,
            [`memberList[${modalIndex}]`]:modalItem
          })
        }
        
      }
      utils.showToast('操作成功','none')
    })
  },
  goUrl(e){
    let url = e.currentTarget.dataset.url
    utils.goUrl(url)
    this.setData({
      showModal:false
    })
  },
})